﻿using SD.Common;
using SD.Infrastructure.EntityBase;
using System;
using System.Collections.Generic;
using System.Linq;
using SlamDunk.ValueObjects.Enums;

namespace SlamDunk.MES.Domain.Entities.ExecutionContext
{
    /// <summary>
    /// 工单实际物料
    /// </summary>
    public class WorkOrderActualMaterial : PlainEntity
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected WorkOrderActualMaterial()
        {
            //初始化导航属性
            this.RelatedBarcodes = new HashSet<WorkOrderActualMaterialBarcode>();
        }
        #endregion

        #region 01.创建工单实际物料构造器
        /// <summary>
        /// 创建工单实际物料构造器
        /// </summary>
        /// <param name="materialId">物料定义Id</param>
        /// <param name="materialLotId">物料批次Id</param>
        /// <param name="directionType">物料输入/输出类型</param>
        /// <param name="quantity">数量</param>
        /// <param name="sort">排序</param>
        /// <param name="description">描述</param>
        public WorkOrderActualMaterial(Guid materialId, Guid? materialLotId, MaterialDirectionType directionType, decimal? quantity, int sort, string description)
            : this()
        {
            this.MaterialId = materialId;
            this.MaterialLotId = materialLotId;
            this.DirectionType = directionType;
            this.Quantity = quantity;
            this.Sort = sort;
            this.Description = description;
        }
        #endregion

        #endregion

        #region # 属性

        #region 物料定义Id —— Guid MaterialId
        /// <summary>
        /// 物料定义Id
        /// </summary>
        public Guid MaterialId { get; private set; }
        #endregion

        #region 物料批次Id —— Guid? MaterialLotId
        /// <summary>
        /// 物料批次Id
        /// </summary>
        public Guid? MaterialLotId { get; private set; }
        #endregion

        #region 物料输入/输出类型 —— MaterialDirectionType DirectionType
        /// <summary>
        /// 物料输入/输出类型
        /// </summary>
        public MaterialDirectionType DirectionType { get; private set; }
        #endregion

        #region 数量 —— decimal? Quantity
        /// <summary>
        /// 数量
        /// </summary>
        public decimal? Quantity { get; private set; }
        #endregion

        #region 排序 —— int Sort
        /// <summary>
        /// 排序
        /// </summary>
        public int Sort { get; private set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; private set; }
        #endregion

        #region 只读属性 - 工单实际物料编号集 —— IEnumerable<string> RelatedBarcodeNos
        /// <summary>
        /// 只读属性 - 工单实际物料编号集
        /// </summary>
        public IEnumerable<string> RelatedBarcodeNos
        {
            get => this.RelatedBarcodes.Select(x => x.MaterialBarcodeNo);
        }
        #endregion

        #region 导航属性 - 工单 —— WorkOrder WorkOrder
        /// <summary>
        /// 导航属性 - 工单
        /// </summary>
        public virtual WorkOrder WorkOrder { get; internal set; }
        #endregion

        #region 导航属性 - 工单实际物料条码集 —— ICollection<WorkOrderActualMaterialBarcode> RelatedBarcodes
        /// <summary>
        /// 导航属性 - 工单实际物料条码集
        /// </summary>
        public virtual ICollection<WorkOrderActualMaterialBarcode> RelatedBarcodes { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 关联工单实际物料条码 —— void RelateBarcodes(IEnumerable<WorkOrderActualMaterialBarcode>...
        /// <summary>
        /// 关联工单实际物料条码
        /// </summary>
        /// <param name="stockOrderBarcodes">工单实际物料条码集</param>
        public void RelateBarcodes(IEnumerable<WorkOrderActualMaterialBarcode> stockOrderBarcodes)
        {
            stockOrderBarcodes = stockOrderBarcodes?.ToArray() ?? new WorkOrderActualMaterialBarcode[0];

            this.ClearRelatedBarcodes();
            if (stockOrderBarcodes.Any())
            {
                this.RelatedBarcodes.AddRange(stockOrderBarcodes);
                foreach (WorkOrderActualMaterialBarcode stockOrderBarcode in stockOrderBarcodes)
                {
                    stockOrderBarcode.WorkOrderActualMaterial = this;
                }
            }
        }
        #endregion

        #region 清空工单实际物料条码 —— void ClearRelatedBarcodes()
        /// <summary>
        /// 清空工单实际物料条码
        /// </summary>
        private void ClearRelatedBarcodes()
        {
            foreach (WorkOrderActualMaterialBarcode relatedBarcode in this.RelatedBarcodes.ToArray())
            {
                this.RelatedBarcodes.Remove(relatedBarcode);
            }
        }
        #endregion

        #endregion
    }
}
